package cn.dyp.com.logprint.ui;

import java.net.UnknownHostException;

import android.app.Activity;
import android.content.Intent;
import android.net.wifi.WifiInfo;
import android.net.wifi.WifiManager;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.RelativeLayout;
import android.widget.Switch;
import android.widget.TextView;

import cn.dyp.com.logprint.R;
import cn.dyp.com.logprint.serial.SerialportService;
import cn.dyp.com.logprint.service.NetServerService;
import cn.dyp.com.logprint.util.Util;


/**
 * 主页面
 * 
 * 
 */
public class MainActivity extends Activity {
	private static final String TAG = "MainActivity";
	TextView ip;
	Switch serverSwitch;
	private Button showLogBtn;
	RelativeLayout server_bg_lay;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		Log.d(TAG, "onCreate");
		setContentView(R.layout.activity_main);
		ip = (TextView) findViewById(R.id.ip);
		serverSwitch = (Switch) findViewById(R.id.server_switch);
		server_bg_lay = (RelativeLayout) findViewById(R.id.server_bg_lay);

		server_bg_lay.setBackgroundResource(R.drawable.server_bg);

		try {
			ip.setText("I P 地址：" + getLocalIpAddress());
		} catch (UnknownHostException e) {
			e.printStackTrace();
		}

		if (Util.isServiceExisted(MainActivity.this,
				NetServerService.class.getName())) {
			serverSwitch.setChecked(true);
		} else {
			serverSwitch.setChecked(false);
		}

		serverSwitch.setOnCheckedChangeListener(new OnCheckedChangeListener() {
			@Override
			public void onCheckedChanged(CompoundButton buttonView,
					boolean isChecked) {
				Intent sev = new Intent(MainActivity.this,
						NetServerService.class);

				if (isChecked) {
					if (!Util.isServiceExisted(MainActivity.this,
							NetServerService.class.getName())) {
						SerialportService.ifSIMULATE = false;
						startService(sev);// 打开Net服务
						sev = new Intent(MainActivity.this,
								SerialportService.class);
						startService(sev);// 启动串口设备
					}
				} else {
					stopService(sev);// 关闭Net服务
					sev = new Intent(MainActivity.this, SerialportService.class);
					stopService(sev);// 关闭串口
				}

			}
		});

		showLogBtn = (Button) findViewById(R.id.showLogButton);
		showLogBtn.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				Intent intent = new Intent(MainActivity.this,
						LogListActivity.class);
				startActivity(intent);
			}
		});
	}

	public static int chose = 0;

	// 获取本地ip地址
	private String getLocalIpAddress() throws UnknownHostException {
		WifiManager wifiManager = (WifiManager) getApplicationContext().getSystemService(WIFI_SERVICE);
		WifiInfo wifiInfo = wifiManager.getConnectionInfo();
		String IP;
		int ipAddress = wifiInfo.getIpAddress();
		IP = intToIp(ipAddress);
		return IP;
	}

	// ip地址转化成字符串
	private String intToIp(int i) {
		return ((i & 0xFF) + " . " + ((i >> 8) & 0xFF) + " . "
				+ ((i >> 16) & 0xFF) + " . " + ((i >> 24) & 0xFF));
	}

	@Override
	protected void onDestroy() {
		super.onDestroy();
		Log.d(TAG, "onDestroy");
	}

	// 定义串口设备列表
	public static String[] devices = new String[] { "/dev/ttySAC0",
			"/dev/ttySAC1", "/dev/ttySAC2", "/dev/ttySAC3" };
}
